<?php
require_once('lib/utils.php');
require_once('lib/member.php');
require_once('lib/category.php');
require_once('lib/gymnasium.php');
require_once('lib/gymhours.php');

// admin/modo
$is_admin = $_SESSION['status_code'] == 'ADMIN';

$categories = cat_get_list();
$creneau = isset($_GET['idc']) ? intval($_GET['idc']) : false;
$gym = isset($_GET['idg']) ? intval($_GET['idg']) : false;

?>
<script src="pages_admin/adherents/adherents.js" type="text/javascript"></script>
<div id="creneaux">
	<h3>Liste des demandes de créneaux</h3>
	<?php
	if (!mysql_num_rows($categories)) {
		echo '<p class="info">Il n\'y a aucun créneaux horaire pour le moment</p>';
	}
	elseif ($creneau && $gym) {
		$members = cat_get_members($creneau, $gym);
		$count = mysql_num_rows($members);
		$gym_data = gym_get_by_id($gym);
		echo '<p class="back"><a href="?p=adherents&r=creneaux">Revenir à la liste des créneaux</a></p>';
		echo '<p style="margin-top:20px; line-height:2;">Liste des membres classés par date de demande<br>
				Créneau : <strong>'.cat_get_name($creneau).'</strong><br />
				Gymnase : <strong>'.$gym_data['name'].'</strong></p>';
		if(!$count)
			echo '<p class="warning">Il n\'y a actuellement aucune demande pour ce créneau</p>';
		else {
			?>
			<table class="dataTable">
			<thead>
				<tr>
					<th>Nom</th>
					<th>Prénom</th>
					<th>Inscrit ?</th>
					<th>Âge</th>
					<th>Date demande inscription</th>
					<th>Actions</th>
				</tr>
			</thead>
			<tbody>
			<?php
			while($member = mysql_fetch_assoc($members)) {
				$img = $member['status_code'] == 'MEMBER' ? 'tick' : 'cross';
				echo '
				<tr id="ligne'.$member['id'].'">
					<td>'.$member['name'].'</td>
					<td>'.$member['firstname'].'</td>
					<td><span class="to_change"><img src="images/icons/'.$img.'.png" alt="'.$img.'" /></span></td>
					<td>'.get_age($member['birth']).' ans</td>
					<td><span>'.time_to_str($member['date_pre_registration']).'</span></td>
					<td>
						<a href="javascript:details('.$member['id'].');" title="Voir les détails du membre">
							<img src="images/icons/magnifier.png" alt="Details" />
						</a>';

						if($member['status_code'] == 'WAITING_MEMBER' && $is_admin)
							echo '
							<a href="javascript:confirm(2,'.$member['id'].');" title="Valider l\'inscription d\'un membre">
								<img class="to_remove" src="images/icons/accept.png" alt="Valider" />
							</a>

							<div id="confirm2'.$member['id'].'" class="hidden decalhaut">
								<div class="decalbas">Valider ?</div>
								<a href="javascript:valider('.$member['id'].', 1);" class="b-oui"></a>
								<a href="javascript:void(0);" class="cacher b-non"></a>
							</div>';
						echo '
					</td>
				</tr>';
			}
			echo '</table><br />
			<p class="tick">Total : <strong>'.$count.'</strong></p>';
		}
	}
	else {
		?>
		<p>Les demandes de créneaux prennent en compte les membres inscrits ainsi que les membres en attente de validation.</p>
		<table class="dataTable">
		<thead>
		<tr>
			<th>Créneau</th>
			<?php
			$gym_ids = array();
			$gyms = gym_get_list();
			while ($row = mysql_fetch_assoc($gyms)) {
				echo '<th>'.$row['name'].'</th>';
				$gym_ids[] = $row['id'];
			}
			?>
		</tr>
		</thead>
		<tbody>
		<?php
		$hours = hours_get_list();
		foreach ($hours as $hour) {
			echo '
			<tr class="numeric">
				<td>
					<strong>'.$hour[1].'</strong><br />
					<em>Age</em> : '.$hour[2].'
				</td>';
				for ($i = 5; $i < count($hour); $i += 2) {
					echo '<td class="numeric">';
					if ($hour[$i + 1]) {
						$count = hours_count_members($hour[$i], $hour[0]);

						if ($count > 0) {
							echo '<a href="'.$_SERVER['REQUEST_URI'].'&amp;idc='.$hour[0].'&amp;idg='.$hour[$i].'" class="right" title="Consulter la liste des demandeurs">
								'.icon_tag('magnifier').'
							</a>';
						}
						else {
							echo '<span class="right">'.icon_tag('magnifier_unavailable').'</span>';
						}
						$reserved = $count.' / '.($hour[4] ? $hour[4] : '?');
						if ($hour[4] && $count >= $hour[4]) {
							echo '<strong style="color: red;">'.$reserved.'</strong>';
						}
						else {
							echo '<strong>'.$reserved.'</strong>';
						}
						echo '<br />'.$hour[$i + 1];
					}
					else {
						echo '<em>pas de créneau</em>';
					}
					echo '</td>';
				}
			echo '
			</tr>';
		}
		?>
		</tbody>
		</table>
		<?php
	}
	?>
</div>
